/*
 * @Author: 码上talk|RC
 * @Date: 2021-08-04 17:34:56
 * @LastEditTime: 2022-02-28 15:47:15
 * @LastEditors: 码上talk|RC
 * @Description: 
 * @FilePath: /ma-distributor/utils/scan-nav-manager.js
 * @微信:  13680065830
 * @邮箱:  3189482282@qq.com
 * @oops: Just do what I think it is right
 */
import { get } from 'lodash'
import * as uniApi from '@/utils/uni';
import {
  localStorage
} from '../utils/localStorage';

class ScanNavType {
  static get PURCHANSE_INFO () { return 1 } // 扫二维码 进入订单详情
}
class ScanNavManager {
  static handleScanCode (options) {
    return new Promise((resolve, reject) => {
      let scene = get(options, 'scene', '')
      if (scene) {
        // has scene
        let sceneURIComponent = get(options, 'query.scene', '') || ''
        if (!sceneURIComponent) {
          reject()
        } else {
          let sceneDataStr = decodeURIComponent(sceneURIComponent);
          let data = {
            scene: scene
          }
          if (typeof sceneDataStr == 'string') {
            var arrPara = sceneDataStr.split('&')
            for (var i in arrPara) {
              let arr = arrPara[i].split('=');
              data[arr[0]] = arr[1]
            }
          } else {
            data = Object.assign(data, options.query)
          }
          resolve(data)
        }
      } else {
        // no scene
        reject('no scene')
      }
    }).then((data) => {
      ScanNavManager.navByData(data)
      return data
    }).catch(() => {
    })
  }
  static navByData (data) {
    if (data.type == ScanNavType.PURCHANSE_INFO && localStorage.getToken()) {
      uniApi.nav('/subpackage/order/pages/purchanse/_id?id=' + data.id)
    }
  }
  constructor () {

  }

  handleScanCode (options) {
    return ScanNavManager.handleScanCode(options)
  }
}

let scanNavManager = new ScanNavManager()
export { ScanNavManager }
export default scanNavManager